package model

import (
	"cloudmusic/config"
)

type Tags struct {
	Id      int64
	TagName string
	Pid     int64
}

func (t Tags) InsertGetID() (id int64, err error) {
	res, err := config.Db.Exec("insert into cm_tag(tagName,pid) values (?,?)", t.TagName, t.Pid)
	if err != nil {
		err = config.Db.QueryRow("select id from cm_tag where tagName = ?", t.TagName).Scan(&id)
		if err != nil {
			return 0, err
		}
		return id, nil
	}
	id, _ = res.LastInsertId()
	return id, nil
}

func GetIDbyTagName(name string) (id int64, err error) {
	err = config.Db.QueryRow("select id from cm_tag where tagName = ?", name).Scan(&id)
	return
}

func GetTagNameList() (list []string, err error) {
	var tagName string
	rows, err := config.Db.Query("select tagName from cm_tag where pid <> 0")
	if err == nil {
		for rows.Next() {
			rows.Scan(&tagName)
			list = append(list, tagName)
		}
	}
	rows.Close()
	return
}
